package web.Servlet;

import domain.message;
import domain.student;
import service.Serviceimpl.SudentServiceImpl;
import service.Serviceimpl.messageServiceImpl;
import service.messageService;
import service.studentService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class StudentServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

      if(request.getSession().getAttribute("flag")==null){
          String user = request.getParameter("user");
          String password = request.getParameter("password");

          if(user==null||password==null){
              response.sendRedirect("login.jsp");
              return;
          }

          studentService studentService = new SudentServiceImpl();
          messageService messageService = new messageServiceImpl();
          try {
              student login = studentService.login(new student(user, password));
              if(login.getS_name().equals("超级管理员")){
                  List<student> students = studentService.finduserSolr();
                  request.getSession().setAttribute("students",students);
                  request.getSession().setAttribute("flag2",1);
                  request.getSession().setAttribute("flag",1);
                  request.getRequestDispatcher("root/RootSuccess.jsp").forward(request,response);
                  return;
              }
              if(login.getS_name()!=null){

                  /*已经取出了name属性*/

                  request.getSession().setAttribute("flag",1);
                  /*传值到数据库*/
                  Date date = new Date();
                  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                  String cc=sdf.format(date);
                  String localIp=null;
                  if (request.getHeader("x-forwarded-for") == null) {
                      localIp = request.getRemoteAddr();
                  }else {
                      localIp= request.getHeader("x-forwarded-for");
                  }
                  String header = request.getHeader("User-Agent");
                  String ie = getIe(header);
                  int sid = login.getS_id();
                  messageService.insert(new message(cc,localIp,ie,sid));

                  /*登录次数*/
                  int count = messageService.count(new message(sid));

                  /*传到页面*/
                  request.getSession().setAttribute("ip",localIp);
                  request.getSession().setAttribute("ie",ie);
                  request.getSession().setAttribute("sj",cc);
                  request.getSession().setAttribute("cs",count);

                  //如果想要在servlet里面调用session对象
                  /*要从web文件夹为起点*/
                  /*知道用户想要姓名属性*/


                  String s_name = login.getS_name();
                  System.out.println(s_name);
                  request.getSession().setAttribute("sname",s_name);
                  request.getRequestDispatcher("success.jsp").forward(request,response);
              }else{
                  response.sendRedirect("login.jsp");
              }
          } catch (SQLException e) {
              e.printStackTrace();
          }
      }else if(request.getSession().getAttribute("flag2")!=null){
          request.getRequestDispatcher("root/RootSuccess.jsp").forward(request,response);
      }else{
          response.sendRedirect("success.jsp");
      }




    }
    public static String getIe(String str){
        if (str.indexOf("Opera") > -1) {      //Opera
            return "Opera";
        }
        if (str.indexOf("Firefox") > -1) {     //Firefox
            return "FF";
        }
        if (str.indexOf("Chrome") > -1){   //Chrome
            return "Chrome";
        }
        if (str.indexOf("Safari") > -1) {     //Safari
            return "Safari";
        }
        if (str.indexOf("compatible") > -1 && str.indexOf("MSIE") > -1) {
            return "IE";
        }        //IE
        return null;
    }

    
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doGet(req,resp);
    }
}
